package com.PVZ.engine.map;

import com.PVZ.model.Entity;
import com.PVZ.model.bullet.Bullet;
import com.PVZ.model.bullet.Sun;
import com.PVZ.model.interfaces.Gettable;
import com.PVZ.model.plant.Plant;
import com.PVZ.model.zombie.Zombie;
import com.PVZ.utils.Util;
import lombok.Data;

import java.awt.*;
import java.util.Collection;

/**
 * Created by @Author: kabuda on @Date: 2021/5/18 20:56
 *
 * Map 类 保存所有的MapBlock 以及地图图片
 */
@Data
public class GameMap {

    private String backGroundSrc;

    //地图有几行
    public static int MAX_ROW = 5;

    //总列数
    public static final int TOTAL_COLUMN = 15;

    public static MapBlock[][] blocks= new MapBlock[MAX_ROW][TOTAL_COLUMN];

    public static final int startX = 150, startY = 80;

    public GameMap(String backGroundSrc, int row){
        MAX_ROW = row;
        this.backGroundSrc = "res/img/background/"+backGroundSrc;
    }

    /**
     * 初始化地图
     * 将地图分成一个个 MapBlock单元格
     */
    public void mapInitial(){
        System.out.println("地图初始化中");


        int curX,curY = startY;
        for(int i = 0; i< MAX_ROW; i++){
            curX = startX;
            for(int j = 0; j< TOTAL_COLUMN; j++){
                // i j分别代表单元格是第几行第几列
                MapBlock block = new MapBlock(new Point(curX,curY),i,j);
                blocks[i][j] = block;

                //X 坐标增加80到下一个单元格
                curX+=MapBlock.width;
            }
            curY+=MapBlock.height;
        }

    }

    public void clearBlocks(){
        for (MapBlock[] blockList : GameMap.blocks) {
            for (MapBlock block : blockList) {
                block.clearEntities();
            }
        }
    }


    /**
     * 更新地图状况 将实体放到单元格中
     */
    public void addEntitiesToBlock(Collection<Entity> entities){
        for (Entity entity : entities) {
            MapBlock block = Util.getBlockByPoint(entity.getPoint());
            if(block==null){
                return;
            }
            block.addEntity(entity);
            // addEntityToBlock(entity);
        }
    }

    /**
     * 通过坐标获取单元格
     * @param row 行
     * @param col 列
     * @return 单元格
     */
    public static MapBlock getBlockByCoor(int row,int col){
        return blocks[row][col];
    }
}
